#!/usr/bin/env python3
"""
Test script to debug the API error
"""
import asyncio
import sys
import traceback
from app.services.locator_service import LocatorService
from app.routers.locate import LocateRequest

async def test_api():
    """Test the API with the same data from the frontend"""
    try:
        # Create test request data with a simple, accessible URL
        request_data = {
            "url": "https://httpbin.org/html",
            "xpath": "//h1",
            "css": "h1"
        }
        
        print("Creating LocateRequest...")
        request = LocateRequest(**request_data)
        print(f"Request created: {request}")
        
        print("Creating LocatorService...")
        service = LocatorService()
        
        print("Processing request...")
        result = await service.process_request(request)
        
        print("Success! Result:")
        print(f"Original HTML: {result.original_node_html[:100]}...")
        print(f"Candidates count: {len(result.candidates)}")
        
    except Exception as e:
        print(f"Error occurred: {e}")
        print("Full traceback:")
        traceback.print_exc()

if __name__ == "__main__":
    asyncio.run(test_api())